/**
 Do not return anything, modify nums in-place instead.
 */
function rotate(nums: number[], k: number): void {
    const n = nums.length;
    k = k % n;
    if (k === 0) return;

    const rotated = new Array(n);
    for (let i = 0; i < n; i++) {
        rotated[(i + k) % n] = nums[i];
    }

    for (let i = 0; i < n; i++) {
        nums[i] = rotated[i];
    }
};
function rotate1(nums: number[], k: number): void {
    const n = nums.length;
    k = k % n;
    if (k === 0) return;

    // 辅助函数：反转数组指定区间
    const reverse = (arr: number[], start: number, end: number) => {
        while (start < end) {
            [arr[start], arr[end]] = [arr[end], arr[start]];
            start++;
            end--;
        }
    };

    // 三步翻转法
    reverse(nums, 0, n - 1);     // 整体翻转
    reverse(nums, 0, k - 1);     // 翻转前k个
    reverse(nums, k, n - 1);     // 翻转剩余部分
}
function fn(nums: number[], k: number): void {
    const n = nums.length;
    k = k % n;
    if (k === 0) return;

    nums.reverse();// 整体翻转

    const reverse1 = (arr: number[], start: number, end: number) => {
        while (start < end) {
            [arr[start], arr[end]] = [arr[end], arr[start]];
            start++;
            end--;
        }
    }
    reverse1(nums, 0, k - 1);// 翻转前k个
    reverse1(nums, k, n - 1);// 翻转剩余部分
}